package com.wm.walmart.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wm.walmart.entity.User;
import com.wm.walmart.mapper.UserMapper;
import com.wm.walmart.param.UserParam;
import com.wm.walmart.service.IUserService;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xieyize
 * @since 2019-12-09
 */
@RestController
@RequestMapping("/user/")
public class UserController {

	@Resource
	private IUserService userService;

	@Resource
	private UserMapper uMapper;

	private Map<String, Object> map = new HashMap<String, Object>();

	/**
	 * 添加用户 http://localhost:8080/walmart/user/insertUser?uName=张三&uSex=男&uAge=15
	 */
	@RequestMapping("insertUser")
	public Map<String, Object> insertUser(User user) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String time = user.getUBir();
		try {
			Date bir = sdf.parse(time);

			Calendar c = Calendar.getInstance();
			int year = c.get(Calendar.YEAR);
			c.setTime(bir);
			user.setUAge(year - c.get(Calendar.YEAR));

		} catch (ParseException e) {
			e.printStackTrace();
		}

		Boolean u = userService.save(user);
		map.clear();
		if (u == true) {
			map.put("success", 1);
		} else {
			map.put("success", -1);
		}
		return map;
	}

	/**
	 * 根据id修改用户
	 * http://localhost:8080/walmart/user/updateUser?uId=9&uName=张三1&uSex=男&uAge=16
	 */
	@RequestMapping("updateUser")
	public Object updateUser(User user) {
		return userService.updateById(user);
	}

	/**
	 * 根据id删除用户 http://localhost:8080/walmart/user/deleteUserById?uId=9
	 */
	@RequestMapping("deleteUserById")
	public Object deleteUserById(Integer uId) {
		return userService.removeById(uId);
	}

	/**
	 * 单表的多条件分页查询
	 * http://localhost:8080/user/findAllByCon?name="a"&age=17&pageIndex=2&pageSize=2
	 * 
	 * @return
	 */
	@RequestMapping("/findAllByCon")
	public Object findAllByCon(UserParam userParam) {
		QueryWrapper<User> qw = new QueryWrapper<User>();
		if (userParam.getUName() != null && !"".equals(userParam.getUName())) {
			qw.like("uName", userParam.getUName());
		}
		if (userParam.getBeginDate() != null && !"".equals(userParam.getBeginDate())) {
			qw.gt("createTime", userParam.getBeginDate());
		}
		if (userParam.getEndDate() != null && !"".equals(userParam.getEndDate())) {
			qw.lt("createTime", userParam.getEndDate());
		}
		if (userParam.getIsLock() != null && !"".equals(userParam.getIsLock())) {
			qw.eq("isLock", userParam.getIsLock());
		}
		IPage<User> page = new Page<User>(userParam.getPage(), userParam.getRows());
		IPage<User> ipage = userService.page(page, qw);
		return ipage.getRecords();
	}

	/**
	 * 根据用户ID锁定该用户
	 */
	@RequestMapping("lockUser")
	public Map<String, Object> updateLockOutById(Integer uId) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		User user = uMapper.selectById(uId);// 根据用户id获取用户信息
		map.put("loginName", user.getUName());
		int i = userService.updateLockOutById(uId, sdf.format(new Date()));// 该用户被锁定的时间
		if (i > 0) {
			map.put("success", i);
		}
		return map;
	}

	/**
	 * 根据用户ID解锁该用户
	 */
	@RequestMapping("unLockUser")
	public int updateUnLockOutById(Integer uId) {
		return userService.updateUnLockOutById(uId);
	}

	/**
	 * 根据用户ID重置解锁该用户密码
	 */
	@RequestMapping("resetPassword")
	public int resetPassword(Integer uId) {
		User user = new User();
		user.setUPword("000000");
		user.setUId(uId);
		Boolean i = userService.updateById(user);
		if (i == true) {
			return 1;
		}
		return -1;
	}

}
